package com.dk.controller;

import com.alibaba.fastjson.JSONObject;
import com.dk.pojo.Logger;
import com.dk.pojo.User;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.LoginService;
import com.dk.utils.JwtUtil;
import com.dk.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("login")
@Api(tags = "登录接口文档")
public class LoginController {

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @PostMapping("loginGo")
    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "password",value = "密码",required = true),
            @ApiImplicitParam(name = "username",value = "用户名",required = true)
    })
    public Result login(String username,String password,HttpServletRequest request){
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //封装数据
        UsernamePasswordToken upToken = new UsernamePasswordToken(username, password);
        //认证
        try {
            subject.login(upToken);
        } catch (UnknownAccountException e) {
            return new Result(ResultConstant.USERNAME_PASSWORD_NULL_ERROR_CODE,
                    ResultConstant.USERNAME_PASSWORD_NULL_ERROR_DATA);
        }catch (IncorrectCredentialsException e) {
            return new Result(ResultConstant.USERNAME_PASSWORD_ERROR_CODE,
                    ResultConstant.USERNAME_PASSWORD_ERROR_DATA);
        }

        //单点登录
        //获取当前登录的账户
        User user =(User) subject.getPrincipal();
        //脱敏
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        //发送消息
        Logger logger = new Logger();
        logger.setLogName(userVO.getUsername());
        logger.setLogDate(new Date());
        logger.setLogState(1);
        String userAgent = request.getHeader("User-Agent");
        if(userAgent.contains("Chrome")) {
            // Chrome浏览器
            logger.setLogBrowser("Chrome浏览器");
        } else if(userAgent.contains("Firefox")) {
            // Firefox浏览器
            logger.setLogBrowser("Firefox浏览器");
        } else if(userAgent.contains("Safari")) {
            // Safari浏览器
            logger.setLogBrowser("Safari浏览器");
        } else if(userAgent.contains("Edge")) {
            // Edge浏览器
            logger.setLogBrowser("Edge浏览器");
        } else if(userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            // IE浏览器
            logger.setLogBrowser("IE浏览器");
        } else {
            // 其他浏览器
            logger.setLogBrowser("其他浏览器");
        }
        String loggerStr = JSONObject.toJSONString(logger);
        rocketMQTemplate.convertAndSend("dk2209-topic",loggerStr);
        //生成token
        String jwt = jwtUtil.createJwt(userVO.getUid().toString(), JSONObject.toJSONString(userVO), "roles");
        return new Result(ResultConstant.LOGIN_SUCCESS_CODE,
                ResultConstant.LOGIN_SUCCESS_DATA,jwt);

    }

}